﻿using System.Linq;
using System.Web.Mvc;
using WRSManagementSystem.DomainModel.Entities;
using WRSManagementSystem.DomainModel.Repositories;
using System;

namespace WRSManagementSystem.Controllers
{
    public class TaskController : Controller
    {
        //
        // GET: /Task/

        private readonly ITasksRepository _taskrep;
        private readonly IProjectsRespoitory _projectrep;

        public TaskController(ITasksRepository taskrep, IProjectsRespoitory projectrep)
        {
            _taskrep = taskrep;
            _projectrep = projectrep;
        }

        public ActionResult Index()
        {
            return View(_taskrep.Find());
        }

        //
        // GET: /Task/Details/5

        public ActionResult Details(int id)
        {
            return View(_taskrep.FindOne(t => t.Id == id));
        }

        //
        // GET: /Task/Create

        public ActionResult Create()
        {
            ViewData["Projects"] = GetProjects();
            var task = new Task();
            task.StartDate = System.DateTime.Today;
            task.EndDate = System.DateTime.Today;
            return View(task);
        }

        //
        // POST: /Task/Create

        [HttpPost]
        public ActionResult Create(Task task, FormCollection fc)
        {
            ViewData["Projects"] = GetProjects();
            //var s = (fc["StartDate"]).Split(new char[] { '/', '-' });
            //var e = fc["EndDate"].Split(new char[] { '/', '-' });

            //task.StartDate = new System.DateTime(int.Parse(s[2]), int.Parse(s[0]), int.Parse(s[1]));
            //task.EndDate = new System.DateTime(int.Parse(e[2]), int.Parse(e[0]), int.Parse(e[1]));

            Project p = null;
            foreach (var tp in _projectrep.Find())
            {
                p = tp;
                if ((tp.Name) == fc["Creator"])
                    break;
            }
            //model.Creator = _peoplerepo.FindOne(p => (p.Name + " " + p.Surname) == fc["Creator"]);
            task.Project = p;
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid || true)
                {
                    _taskrep.Add(task);
                    //_taskrep.Update(task);
                    _taskrep.Save();
                    return RedirectToAction("Index");
                }
            }
            catch(Exception e)
            {
                return View(task); // tmp for error handle
            }
            ViewData["Projects"] = GetProjects();
            return View(task);
        }

        //
        // GET: /Task/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData["Projects"] = GetProjects();
            return View(_taskrep.FindOne(t => t.Id == id));
        }

        //
        // POST: /Task/Edit/5

        [HttpPost]
        public ActionResult Edit(Task model)
        {
            ViewData["Projects"] = GetProjects();
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    _taskrep.Update(model);
                    _taskrep.Save();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
            return View(model);
        }

        public SelectList GetProjects()
        {
            return new SelectList(_projectrep.Find(), "Id", "Name");
        }

        //
        // GET: /Task/Report

        public ActionResult Report()
        {
            string s = Request.Params["deadline"];
            System.DateTime endTime;
            if (!System.DateTime.TryParse(s, out endTime))
                endTime = System.DateTime.Now;
            var a = _taskrep.Find().Where(t => (t.EndDate.CompareTo(endTime) < 0));
            return View(a.Where(t => t.IsActive));
        }
    }
}